﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：ScriptHelper.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年5月24日
 */
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ajayumi.Platform.Web
{
    /// <summary>
    /// ScriptHelper.cs 脚本辅助类
    /// Author   : 黄乙冬
    /// Date     : 2011-12-01
    /// </summary>
    public class ScriptHelper
    {
        public const string SCRIPTKEY = "PageScript";
        //JQuery脚本结构的字符串
        public const string JQUERY_STRUCTURE = "<script type=\"text/javascript\">$(document).ready(function () {{ {0} }});</script>";

        /// <summary>
        /// 向页面添加注册的脚本
        /// </summary>
        /// <param name="scriptStr"></param>
        public static void AddScript(string scriptStr)
        {
            AddScript(SCRIPTKEY, scriptStr);
        }

        /// <summary>
        /// 向页面添加注册的脚本
        /// </summary>
        /// <param name="key"></param>
        /// <param name="scriptStr"></param>
        public static void AddScript(string key, string scriptStr)
        {
            string jsStr = string.Format(JQUERY_STRUCTURE, scriptStr);

            // Gets the executing web page
            Page page = HttpContext.Current.CurrentHandler as Page;
            page.ClientScript.RegisterStartupScript(page.GetType(), key, jsStr);
        }

        public static void AddScript(Literal l, string scriptStr)
        {
            string jsStr = string.Format(JQUERY_STRUCTURE, scriptStr);
            l.Text = jsStr;
        }

        public static void AddScript(HiddenField hf, string scriptStr)
        {
            StringBuilder sbScript = new StringBuilder();
            sbScript.Append("$(document).ready(function () {");
            sbScript.Append(scriptStr);
            sbScript.Append("});");
            hf.Value = sbScript.ToString();
        }
    }
}
